package lk.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * author lk
 *
 * @date 2021/10/27 9:54
 * @description 密码工具类 描述
 * @since 1.0
 */
public class PasswordUtil {

    private static final Logger log = LoggerFactory.getLogger(PasswordUtil.class);

    private PasswordUtil() {
        log.error("不能创建私有化构造器");
        throw new IllegalStateException("Utility class");
    }


    /**
     * 获取到加密器 后续如果加密方式改变也可以不需要修改此工具
     *
     * @return
     */
    public static PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    /**
     * 加密
     *
     * @param originalPassword 原始密码
     * @return 加密后的密码
     */
    public static String encryptionPassword(String originalPassword) {
        return passwordEncoder().encode(originalPassword);
    }

    /**
     * 比对密码是否正确
     *
     * @param rawPassword     原始密码
     * @param encodedPassword 加密后的密码
     * @return true 为比对成功 false 比对失败
     */
    public static boolean matches(CharSequence rawPassword, String encodedPassword) {
        return passwordEncoder().matches(rawPassword, encodedPassword);
    }

}
